package cn.newtouch._05_xhr;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * 异步方式发送消息
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //1.获取生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer_group");

        //2.设置nameserver地址
        producer.setNamesrvAddr("localhost:9876");

        //3.启动
        producer.start();

        //4.生产消息并发送
        Message message = new Message("xhr", "hei", "学海无涯苦作舟！！".getBytes(StandardCharsets.UTF_8));
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消费消息成功！！"+sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("消费消息失败！！");
            }
        });

        // 等待5s
        //countDownLatch.await(5, TimeUnit.SECONDS);

        //关闭实例
        //producer.shutdown();


    }
}
